package com.sphd.restaurantsystem.activitys.kuncun;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.DateQueryAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.DataNoInfo;
import com.sphd.restaurantsystem.info.InorOutRecordInfo;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.DateQueryPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.DateQueryView;
import com.wzh.frame.utils.TimeUtils;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 按日期查询出入库
 */
public class DateQueryActivity extends BaseActivity implements DateQueryView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.select_data)
    TextView selectData;
    @BindView(R.id.in_record)
    TextView inRecord;
    @BindView(R.id.out_record)
    TextView outRecord;
    @BindView(R.id.sell_record)
    TextView sellRecord;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;

    Intent intent;
    Bundle bundle;
    private RestaurantInfo restaurantInfo;
    private LoginInfo loginInfo;
    private DateQueryPresenterImpl dateQueryPresenter;
    String today;
    DateQueryAdapter dateQueryAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_query);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        mySrf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                addData();
            }
        });
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                bundle.putSerializable(Constant.INOROUTRECODR,(InorOutRecordInfo)baseQuickAdapter.getItem(i));
                intent.putExtras(bundle);
                intent.setClass(context,GoodsActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initAdapter() {
        setRefresh(mySrf);
        setRecy(myRecy);
        dateQueryAdapter = new DateQueryAdapter(R.layout.record_in_out, new ArrayList<>());
        myRecy.setAdapter(dateQueryAdapter);
    }

    private void initView() {
        setTitleBar(toolbar, "按日期查询");
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        dateQueryPresenter = new DateQueryPresenterImpl(this);
        selectData.setText(TimeUtils.getCurrentTimeInString(new SimpleDateFormat("MM月\ndd日")));
        today = TimeUtils.getCurrentTimeInString(TimeUtils.DATE_FORMAT_DATE);
        initAdapter();
        addData();
    }

    private void addData() {
        dateQueryPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.INOROUTFORDAY + "cgid=" + restaurantInfo.getId()
                + "&day=" + today);
    }

    @OnClick(R.id.select_data)
    public void onClick() {
        showdialog();
    }

    private void showdialog() {
        LayoutInflater li = LayoutInflater.from(this);
        View promptsView = li.inflate(R.layout.layout_start_end_time, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);
        alertDialogBuilder.setView(promptsView);
        final DatePicker startDatePicker = (DatePicker) promptsView
                .findViewById(R.id.start_datePicker);
        TextView startTitle = (TextView) promptsView.findViewById(R.id.start_title);

        TextView endTitle = (TextView) promptsView.findViewById(R.id.end_title);
        endTitle.setVisibility(View.GONE);
        final DatePicker endDatePicker = (DatePicker) promptsView
                .findViewById(R.id.end_datePicker);
        endDatePicker.setVisibility(View.GONE);
        startDatePicker.init(startDatePicker.getYear(), startDatePicker.getMonth(), startDatePicker.getDayOfMonth(), new DatePicker.OnDateChangedListener() {

            public void onDateChanged(DatePicker view, int year, int monthOfYear,
                                      int dayOfMonth) {
                Calendar c = Calendar.getInstance();
                c.set(year, monthOfYear, dayOfMonth);
                endDatePicker.init(startDatePicker.getYear() + 1, startDatePicker.getMonth(), startDatePicker.
                        getDayOfMonth(), dateSetListener);
            }
        });

        alertDialogBuilder.setTitle("编辑查询时间");
        // set dialog message
        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                selectData.setText(((startDatePicker.getMonth() + 1) < 10 ? "0" +
                                        (startDatePicker.getMonth() + 1) :
                                        (startDatePicker.getMonth() + 1))
                                        + "月\n" + ((startDatePicker.getDayOfMonth() + 1) < 10 ? "0" +
                                        startDatePicker.getDayOfMonth() :
                                        startDatePicker.getDayOfMonth()) + "日");

                                today = startDatePicker.getYear() + "-" +
                                        ((startDatePicker.getMonth() + 1) < 10 ? "0" +
                                                (startDatePicker.getMonth() + 1) :
                                                (startDatePicker.getMonth() + 1))
                                        + "-" + ((startDatePicker.getDayOfMonth() + 1) < 10 ? "0" +
                                        startDatePicker.getDayOfMonth() :
                                        startDatePicker.getDayOfMonth());
                                addData();
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }

    private DatePicker.OnDateChangedListener dateSetListener = new DatePicker.OnDateChangedListener() {

        public void onDateChanged(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
            Calendar c = Calendar.getInstance();
            c.set(year, monthOfYear, dayOfMonth);
            System.out.println("TEST");

        }
    };

    @Override
    public void recRes(DataNoInfo dataNoInfo, List<InorOutRecordInfo> goodInOrOutInfos) {
        mySrf.setRefreshing(false);
        if (dataNoInfo != null) {
            inRecord.setText("入库\n" + dataNoInfo.getIn_sum_number());
            outRecord.setText("出库\n" + dataNoInfo.getOut_sum_number());
            sellRecord.setText("销售\n" + dataNoInfo.getSell_sum_number());
        } else {
            inRecord.setText("入库\n0");
            outRecord.setText("出库\n0");
            sellRecord.setText("销售\n0");
        }
        dateQueryAdapter.setNewData(goodInOrOutInfos);

    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
